using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;

namespace Voodoo.Meta.RVModules
{
	public class KeyCanvas : MonoBehaviour
	{
		public List<GameObject> disabledKeys;

		public List<GameObject> enableKeys;

		public GameObject root;

		private CancellationTokenSource _cancellationTokenSource;

		private void Awake()
		{
			HideRoot();
			if (ChestRoomKeysManager.Instance != null)
			{
				ChestRoomKeysManager.Instance.onKeyCollected += OnKeyCollected;
			}
		}

		private void OnDestroy()
		{
			if ((bool)ChestRoomKeysManager.Instance)
			{
				ChestRoomKeysManager.Instance.onKeyCollected -= OnKeyCollected;
			}
			_cancellationTokenSource?.Cancel();
		}

		private void OnKeyCollected()
		{
			int currentKeys = ChestRoomKeysManager.Instance.CurrentKeys;
			for (int i = 0; i < disabledKeys.Count; i++)
			{
				bool flag = currentKeys > i;
				disabledKeys[i].SetActive(!flag);
				enableKeys[i].SetActive(flag);
			}
			DelayedHideRoot(3f);
		}

		private async Task DelayedHideRoot(float delay)
		{
			root.SetActive(value: true);
			_cancellationTokenSource = new CancellationTokenSource();
			await Task.Delay(TimeSpan.FromSeconds(delay), _cancellationTokenSource.Token);
			HideRoot();
		}

		private void HideRoot()
		{
			root.SetActive(value: false);
		}
	}
}
